#include "head.h"
void ls(int sockfd,transport_protocol_t* msg) {  

    MYSQL_RES *result1;  
    char ret[1000];
    bzero(ret, sizeof(ret));
    char *buf1 = getaa(msg, ret);

    char sql_buf1[500] = {0};
    sprintf(sql_buf1,
            "select file_id from bd_file where url = '%s' and user_id = %s and file_type =1" 
            ,buf1,msg->user_id);
   // printf("sql_buf:%s\n",sql_buf1);

    if (mysql_query(&msg->my_sql,sql_buf1)){
        fprintf(stderr, "Query failed: %s\n",mysql_error(&msg->my_sql));
        LOG_WRITE(3,"ls的sql查询语句出现错误");
        return;
    }
    result1 = mysql_store_result(&msg->my_sql);
    if (!result1) {
        fprintf(stderr, "Failed to store result: %s\n", mysql_error(&msg->my_sql));
        return;
    }
    //将数据库内容存入buf,并传送到客户端
    char buf2[100] = {0};
    if(!mysql_num_rows(result1))
    {
       // printf("该目录不存在\n");
        strcat(buf2,"该目录不存在");
        send(sockfd, buf2, sizeof(buf2), 0);
        return;
    }
    MYSQL_RES *result;
    MYSQL_ROW row;

    //发送给数据库(文件存在的情况)
    printf("%s\n", mysql_error(&msg->my_sql));
    char sql_buf[500] = {0};
    sprintf(sql_buf,
            "select filename from bd_file where delete_flag = 0 and user_id = %s and (upload_flag <> -1 or upload_flag is NULL) and parent_id = (select file_id from bd_file where url = '%s' and user_id = %s)",
            msg->user_id ,buf1,msg->user_id);
    // printf("sql_buf:%s\n",sql_buf);


    if (mysql_query(&msg->my_sql,sql_buf)){  
        fprintf(stderr, "Query failed: %s\n",mysql_error(&msg->my_sql));  
        LOG_WRITE(3,"ls的sql查询语句出现错误"); 
        return;  
    }  
    result = mysql_store_result(&msg->my_sql);  
    if (!result) {  
        fprintf(stderr, "Failed to store result: %s\n", mysql_error(&msg->my_sql));  
        return;  
    }

    //将数据库内容存入buf,并传送到客户端
    char buf[100] = {0};
    if(!mysql_num_rows(result))
    {
       // printf("目录为空\n");
        strcat(buf,"目录为空");
        LOG_WRITE(1,"目录为空"); 
        send(sockfd, buf, sizeof(buf), 0);
    }
    else{
        while ((row = mysql_fetch_row(result))) {

            for(unsigned int i=0; i<mysql_num_fields(result); i++){

                strncat(buf, " \t ", 4 );

                strncat(buf, row[i], strlen(row[i]));
            }
        }
        strcat(buf, "\n");
        mysql_free_result(result);

        // 把目录信息: 回给客户端
        send(sockfd, buf, sizeof(buf), 0);
    }
}


char* getaa(transport_protocol_t* msg, char *ret)//获取绝对路径
{
    //栈数组
    char *stack[1000];
    //栈顶索引
    int top = -1;

    char buf[100] = { 0 };
    strcat(buf, msg->curr_path);
    if (strcmp(buf, "/") != 0){
        strcat(buf, "/");
    }
    strcat(buf, msg->buf);

    char *token = strtok(buf, "/");

    while(token != NULL){
        if (strcmp(token, ".") == 0){
            token = strtok(NULL,"/");
            continue;
        }else if(strcmp(token, "..") == 0){
            //出栈
            if(top != -1){
                top--;
            }
        }else{
            //入栈
            stack[++top] = (char *)malloc(strlen(token) + 1);
            //判断是否分配成功
            ERROR_CHECK(stack[top], NULL, "malloc error");
            strcpy(stack[top], token); 
        }
        token = strtok(NULL,"/");
    }

    //获取栈中的数组

    //获取总共的长度
    int len = 1;    
    for (int i = 0; i <= top; i++){
        //加2 是因为要加一个/
        len += strlen(stack[i]) + 2;
    }

    char *ret_path = (char*)malloc(len + 2);
    ERROR_CHECK(ret_path, NULL, "malloc error");

    bzero(ret_path, sizeof(ret_path));

    for (int i = 0; i <= top; i++){
        if (i > 0){
            strcat(ret_path, "/");
        }
        strcat(ret_path, stack[i]);
        free(stack[i]);
    }

    char *tmp = (char*)malloc(len + 1);
    ERROR_CHECK(tmp, NULL, "malloc error");
    bzero(tmp, sizeof(tmp));
    strcat(tmp, "/");
    strcat(tmp, ret_path);

    free(ret_path);
    
    strcpy(ret, tmp);
    free(tmp);

    return ret;
}
